package com.yl.ifanr;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;
import java.util.List;

import com.yl.util.Constants;
import com.yl.util.FileLog;
import com.yl.ifanr.R;
import com.yl.ifanr.ActivityMain;
import com.yl.ifanr.db.*;
import com.yl.rss.RssLoad;
import com.yl.rss.RssFeed;

public class StartActivity extends Activity {
	public final String TAG = "StartActivity";
	private Thread thread;
	private RssFeed feed = null;
	private List<RssItemBean> itemList;
	private RssDaoimpl rssDaoimpl;
	private int time = 1;
	
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
       setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        try{
        	setContentView(R.layout.start);
        	rssDaoimpl = new RssDaoimpl(this);
			
        	thread = new Thread(updateThread);
        	thread.start();
        	initConstant();
        }catch(Exception e){
        	Log.e(TAG, e.getMessage());
        }

	}
	/**
	 * 变量初始化
	 */
	private void initConstant(){
		WindowManager manage=getWindowManager();
    	Display display=manage.getDefaultDisplay();
    	Constants.SCREEN_HEIGHT = display.getHeight();
    	Constants.SCREEN_WIDTH = display.getWidth();
    	
    	Constants.SDCARD_PATH = Environment.getExternalStorageDirectory().getAbsolutePath();
    	Constants.PATH_TEMP = "/" + Constants.APP_NAME + "/temp/";
    	Constants.PATH_LOG ="/" + Constants.APP_NAME + "/log/";
    	
	}
	
	Handler updateHandler=new Handler(){
		public void handleMessage(Message msg){
			jumpToMainActivity();
		}
	};
	
	Runnable updateThread = new Runnable(){
		@Override
		public void run() {
			//获取主线程updateHandler的Message
			Message msg=updateHandler.obtainMessage();
			boolean result = false;
			try {
				SharedPreferences config = getSharedPreferences("config", 0);
				int firstLogin = config.getInt("firstLogin", 0);
				
				if(firstLogin==0){
					RssLoad rssload = new RssLoad();
					feed = rssload.getFeed(Constants.RSS_URL);
					itemList = feed.getAllItems();
					result = rssDaoimpl.insert(itemList);
					Log.d(TAG, "更新数据。");
					rssDaoimpl.close();
					if(result){
						config.edit().putInt("firstLogin", 1).commit();
					}
				}
				 
				Thread.sleep(time * 1000);
				
			} catch (Exception e) {
				// TODO: handle exception
				Log.e(TAG, e.getMessage());
			}
			updateHandler.sendMessage(msg);
		}
	};
	
	/**
	 * 跳转
	 */
    private void jumpToMainActivity(){
		try {
			FileLog.log("准备启动主程序");
	    	startActivity(new Intent(this,ActivityMain.class));
	    	
	    	finish();
	    	
		} catch (Exception e) {
			// TODO: handle exception
			Log.e(Constants.TAG, e.getMessage());
		}
		
    }
}
